<?php
error_reporting(E_ERROR | E_PARSE);
session_start();

require_once "Connectors/PlayerWS.php";
require_once "Connectors/CommonWSSearch.php";
include('includes.php');

if (isset($_SESSION['fbuser'])&&$_SESSION['fbuser'] )
{

    $playerws = new PlayerWS();
    $ws = new SearchWS();
    $usrInfo=$ws->GetUserBasicInfo($_SESSION['fbuser_profile']['email'],$_SESSION['fbuser_profile']['email']);

    if(strlen($usrInfo->body->mail)<5)
    {

        if($playerws->RegisterPlayer( $_SESSION['fbuser_profile']['name'],$_SESSION['fbuser_profile']['birthday'],$_SESSION['fbuser_profile']['email'],
            $_SESSION['fbuser_profile']['gender'],$_SESSION['fbuser_profile']['id']."A1@b2#","Unknown","Unknown",""))
        {



            ?>
                <html>
                <head>

                </head>
                <body>
                 -
                <script>
                    jNotify(
                            'Welcome <?php echo $usrInfo->body->name;?> to Sports Around. We will get back to you soon!<br/><div>'+
                                    '<div class="regJugador">'+
                                    '<form method="get" action="comingsoon.php" class="standForm">'+

                                    '<input type="submit" value="Continue"/>'+
                                    '</form>'+
                                    //'<a href="fbclose.php" class="facebookConnect" style="margin:0px 0 22px 53px;">Facebook Logout</a>'+
                                    '</div>'+
                                    '</div>',
                            {
                                autoHide : false, // added in v2.0
                                TimeShown : 7000,
                                HorizontalPosition : 'center',
                                VerticalPosition: 'center'
                               // onCompleted : function(){ // added in v2.0
                                 //   location.replace("comingsoon.php");
                                //}
                            }
                    );
                </script>

                </body>
                </html>

            <?php

        }


    }
    else{
        session_start();
        require 'facebook-php-sdk-master/src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
        $facebook = new Facebook(array(
            'appId'  => '130496170462251',
            'secret' => '7ed6a5d09450be4e6214e35a022f8c8e',
        ));

// Get User ID
        $_SESSION['fbuser'] = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

        if ($_SESSION['fbuser']) {
            try {
// Proceed knowing you have a logged in user who's authenticated.
                $_SESSION['fbuser_profile'] = $facebook->api('/me');

            } catch (FacebookApiException $e) {
                error_log($e);
                $_SESSION['fbuser'] = null;
            }
        }

// Login or logout url will be needed depending on current user state.
        if ($_SESSION['fbuser']) {
            $logoutUrl = $facebook->getLogoutUrl(array(
                'next' => 'http://www.sportsaround.us/fbclose.php'
            ));
        } else {
//$permission= array('scope'=>'user_birthday,email','redirect_uri' => 'http://www.cluvie.com/fb.php');
            $params = array(
                'scope' => 'email,user_birthday,publish_actions,create_event,user_location',
                'redirect_uri' => 'http://www.sportsaround.us/fbclose.php',
            );
            $loginUrl = $facebook->getLoginUrl($params);
        }

        ?>

<html>
<head>

</head>
<body>
-
<script>
    jNotify(
            'You are already a Sports Around user. We will get back to you soon!<br/><div >'+
                    '<div class="regJugador">'+
            '<form method="get" action="comingsoon.php" class="standForm">'+

            '<input type="submit" value="Continue"/>'+

            '</form>'+
            '<form method="get" action="fbclose.php" class="standForm" style="float: right;">'+


            '<input type="submit" value="I want to register another account"/>'+
            '</form>'+
            //'<a href="fbclose.php" class="facebookConnect" style="margin:0px 0 22px 53px;">Facebook Logout</a>'+
            '</div>'+
            '</div>',
            {
                autoHide : false, // added in v2.0
                TimeShown : 7000,
                HorizontalPosition : 'center',
                VerticalPosition: 'center'
                //onCompleted : function(){ // added in v2.0
                //    location.replace("comingsoon.php");
                //}
            }
    );
</script>

</body>
</html>

<?php
}}
else{



require 'facebook-php-sdk-master/src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
    'appId'  => '130496170462251',
    'secret' => '7ed6a5d09450be4e6214e35a022f8c8e',
));

// Get User ID
$_SESSION['fbuser'] = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($_SESSION['fbuser']) {
    try {
// Proceed knowing you have a logged in user who's authenticated.
        $_SESSION['fbuser_profile'] = $facebook->api('/me');

    } catch (FacebookApiException $e) {
        error_log($e);
        $_SESSION['fbuser'] = null;
    }
}

// Login or logout url will be needed depending on current user state.
if ($_SESSION['fbuser']) {
    $logoutUrl = $facebook->getLogoutUrl();
} else {
//$permission= array('scope'=>'user_birthday,email','redirect_uri' => 'http://www.cluvie.com/fb.php');
    $params = array(
        'scope' => 'email,user_birthday,publish_actions,create_event,user_location',
        'redirect_uri' => 'http://www.sportsaround.us/fbsuccess.php',
    );
    $loginUrl = $facebook->getLoginUrl($params);
}
    echo '<h2>Redirecting....</h2>';
    echo("<script type=\"text/javascript\" >

								location.replace(\"".$loginUrl."\");

								</script>");

}
?>